/*
 * @Description:
 * @Author: lei.zheng
 * @Date: 2019-09-21 10:56:52
 * @LastEditTime: 2019-09-21 10:57:52
 * @LastEditors: lei.zheng
 */
import vix from '@bairong/vix'

const { request } = vix

// auth tree
export function getAuthListApi() {
  return request({
    url: '/resourceTree/getAll',
    method: 'post'
  })
}

export function addAuthApi(data: object) {
  return request({
    url: '/resourceTree/save',
    method: 'post',
    data
  })
}

export function updateAuthApi(data: object) {
  return request({
    url: '/resourceTree/update',
    method: 'post',
    data
  })
}

export function deleteAuthApi(data: object) {
  return request({
    url: '/resourceTree/delete',
    method: 'post',
    data
  })
}
// 角色类型管理

export function getRoleTypeResourceApi(data: object) {
  return request({
    url: '/roleType/resource',
    method: 'post',
    data
  })
}
// 角色管理
export function getRoleListApi(data: object) {
  return request({
    url: '/role/list',
    method: 'post',
    data
  })
}

export function addRoleApi(data: object) {
  return request({
    url: '/role/add',
    method: 'post',
    data
  })
}

export function updateRoleApi(data: object) {
  return request({
    url: '/role/update',
    method: 'post',
    removeEmptyFields: false,
    data
  })
}

export function deleteRoleApi(data: object) {
  return request({
    url: '/role/delete',
    method: 'post',
    data
  })
}

export function checkRoleNameApi(data: object) {
  return request({
    url: '/role/name/exist',
    method: 'post',
    data
  })
}

export interface Role {
  id: string
  roleName: string
  roleType: string
}

export function getRoleOptionsApi() {
  return new Promise((reslove, reject) => {
    (request({
      url: '/role/rolelist',
      method: 'post'
    }) as any)
      .then((data: Array<Role>) => {
        const rst = data.map(x => ({
          value: x.id,
          label: x.roleName,
          type: x.roleType
        }))
        reslove(rst)
      })
      .catch((err: Error) => {
        reject(err)
      })
  })
}

export function getUserListApi(data: object) {
  return request({
    url: '/user/list',
    method: 'post',
    data
  })
}

export function checkUserNameApi(data: object) {
  return request({
    url: '/user/name/exist',
    method: 'post',
    data
  })
}

export function addUserApi(data: object) {
  return request({
    url: '/user/add',
    method: 'post',
    data
  })
}

export function updateUserApi(data: object) {
  return request({
    url: '/user/update',
    removeEmptyFields: false,
    method: 'post',
    data
  })
}

export function updateUserStatusApi(data: object) {
  return request({
    url: '/user/forbidden',
    method: 'post',
    data
  })
}

export function deleteUserApi(data: object) {
  return request({
    url: '/user/delete',
    method: 'post',
    data
  })
}

export function disabledUserApi(data: object) {
  return request({
    url: '/user/changeStatus',
    method: 'post',
    data
  })
}

export function getDepartmentOptionsApi() {
  return new Promise((reslove, reject) => {
    (request({
      url: '/department/list',
      method: 'post'
    }) as any)
      .then((data: object) => {
        reslove(data)
      })
      .catch((err: Error) => {
        reject(err)
      })
  })
}

export function getDepartmentListApi(data: object) {
  return request({
    url: '/department/list',
    method: 'post',
    data
  })
}

export function addDepartmentApi(data: object) {
  return request({
    url: '/department/add',
    method: 'post',
    data
  })
}
export function updateDepartmentApi(data: object) {
  return request({
    url: '/department/update',
    method: 'post',
    data
  })
}
export function deleteDepartmentApi(data: object) {
  return request({
    url: '/department/delete',
    method: 'post',
    data
  })
}
